package com.cn.broker.processor;

import com.cn.broker.BrokerController;
import com.cn.common.ClientInfo;
import com.cn.common.processor.INettyRequestProcessor;
import com.cn.common.processor.ProcessorCode;
import com.cn.msg.Message;
import com.cn.util.RemotingSerializable;
import io.netty.channel.ChannelHandlerContext;

import java.util.ArrayList;
import java.util.List;

public class ClientProcessor implements INettyRequestProcessor {

    private BrokerController brokerController;

    public ClientProcessor(BrokerController brokerController) {
        this.brokerController = brokerController;
    }

    @Override
    public Message process(Message message, ChannelHandlerContext ctx) {
        ClientInfo clientInfo = RemotingSerializable.decode(message.getByteBodys(), ClientInfo.class);
        switch (message.getType()) {
            case 17:
                this.brokerController.getConsumerManager().register(clientInfo, ctx.channel());
                return null;
            case 18:
                List<String> list = this.brokerController.getConsumerManager().filterGroup(clientInfo.getGroup());
                byte[] bytes = RemotingSerializable.encode(list);
                message.setByteBodys(bytes);
                message.setLength(bytes.length);
                return message;
        }
        return null;
    }
}
